/*
 * @Author: wuzhouhai wzh200x@126.com
 * @Date: 2024-07-09 11:40:47
 * @LastEditors: wuzhouhai wzh200x@126.com
 * @LastEditTime: 2024-08-28 16:33:44
 * @FilePath: \dwys_web\assets\script\phoenix\PhoenixClient.ts
 * @Description: 
 */
import { _decorator, Component, Node } from 'cc';
import { PhoenixRoot } from '../../../extensions/phoenix-plugin-client/assets/core/PhoenixRoot';
import { oops } from '../../../extensions/oops-plugin-framework/assets/core/Oops';
import { GameEvent } from '../common/event/GameEvent';
import { PhoenixEventDefine } from '../../../extensions/phoenix-plugin-client/assets/core/defines/event.define';
import { AvatarModel } from './models/avatar.model';
import { OpcodeApp } from './define/opcode.define';
import messageManager from '../../../extensions/phoenix-plugin-client/assets/core/managers/message.manager';
import { C2SMessageGetResources } from './message/c2s/c2sMessage_GetResources';
import { S2CMessageGetResources } from './message/s2c/s2cMessage_GetResources';
import { VM } from '../../../extensions/oops-plugin-framework/assets/libs/model-view/ViewModel';
import { EnumGameRes } from './define/gameRes.define';
import { S2CMessageGetRoomInfo } from './message/s2c/s2cMessage_GetRoomInfo';
import { C2SMessageGetRoomInfo } from './message/c2s/c2sMessage_GetRoomInfo';
import { S2CMessageRoomPmtUpLevel } from './message/s2c/s2cMessage_RoomPmtUpLevel';
import { C2SMessageGetRoomPmt } from './message/c2s/c2sMessage_GetRoomPmt';
import { C2SMessageRoomUpLevel } from './message/c2s/c2sMessage_RoomPmtUpLevel';
import { C2SMessageRoomUpCyLevel } from './message/c2s/c2sMessage_RoomUpCyLevel';
import { S2CMessageGetRoomPmt } from './message/s2c/s2cMessage_GetRoomPmt';
import { S2CMessageRoomUpCyLevel } from './message/s2c/s2cMessage_RoomUpCyLevel';
import { S2CMessageUpdateResources } from './message/s2c/s2cMessage_UpdateResources';
import { S2CMessageUpdateRoomInfo } from './message/s2c/s2cMessage_UpdateRoomInfo';
import { S2CMessageUpdateRoomPmt } from './message/s2c/s2cMessage_UpdateRoomPmt';
import { S2CMessageHarvestResources } from './message/s2c/s2cMessage_HarvestResources';
import * as proto from "./protoc/ts/proto_app.js";
import { C2SMessageHarvestResources } from './message/c2s/c2sMessage_HarvestResources';
import { C2SMessageActiveRoom } from './message/c2s/c2sMessage_ActiveRoom';
import { S2CMessageActiveRoom } from './message/s2c/s2cMessage_ActiveRoom';
const { ccclass, property } = _decorator;

@ccclass('PhoenixClient')
export class PhoenixClient extends PhoenixRoot {

    private switchRoom:number = 0;

    onLoad() {
        // 注册phoenix事件
        this.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_SUCCESS, this.onLoginSuccess, this);
        this.registerEvent(PhoenixEventDefine.PHOENIX_LOGIN_FAIL, this.onLoginFail, this);
        this.registerEvent(PhoenixEventDefine.PHOENIX_GAME_LOGIN_SUCCESS, this.onLoginGameSuccess, this);
        this.registerEvent(PhoenixEventDefine.PHOENIX_GAME_LOGIN_FAIL, this.onLoginGameFail, this);
        this.registerEvent(PhoenixEventDefine.PHOENIX_GAME_ACCOUNT_KICK, this.onKick, this)

        // 注册开始登录事件
        oops.message.on(GameEvent.GameStartLogin, this.onHandler, this);
        oops.message.on(GameEvent.StartLoginGameServer, this.onHandler, this);

        // oops.message.on(GameEvent.LoginGameServerSuccess, this.onHandler, this);

        oops.message.on(GameEvent.UpdateGameRes, this.onUpdateGameRes, this);
        oops.message.on(GameEvent.GetRoomInfo, this.onGetRoomInfo, this)
        oops.message.on(GameEvent.RequestGetRoomPmtInfo, this.onRequestRoomPmtInfo, this)
        oops.message.on(GameEvent.GetRoomPmtInfo, this.onGetRoomPmtInfo, this)
        oops.message.on(GameEvent.UpRoomPmtLevel, this.onRoomPmtUpLevel, this)
        oops.message.on(GameEvent.UpRoomCyLevel, this.onRoomUpCyLevel, this)
        oops.message.on(GameEvent.UpdateRoom, this.onUpdateRoomInfo, this);
        oops.message.on(GameEvent.ActiveRoom, this.onActiveRoom, this);

    }

    start(): void {
        super.start();
        // 绑定角色数据模型
        let avatarModel = new AvatarModel();
        VM.add(avatarModel, "AvatarModel");
        console.log("init avatar model");
        this.bindAvatarModel(avatarModel);
        // 注册消息
        this.initMessage();
        this.InitPhoeix();
    }
    
    protected onDestroy(): void {
        oops.message.off(GameEvent.GameStartLogin, this.onHandler, this);
        oops.message.off(GameEvent.StartLoginGameServer, this.onHandler, this);
        // oops.message.off(GameEvent.LoginGameServerSuccess, this.onHandler, this);
        oops.message.off(GameEvent.UpdateGameRes, this.onUpdateGameRes, this);
        oops.message.off(GameEvent.GetRoomInfo, this.onGetRoomInfo, this)
        oops.message.off(GameEvent.RequestGetRoomPmtInfo, this.onRequestRoomPmtInfo, this)
        oops.message.off(GameEvent.GetRoomPmtInfo, this.onGetRoomPmtInfo, this)
        oops.message.off(GameEvent.UpRoomPmtLevel, this.onRoomPmtUpLevel, this)
        oops.message.off(GameEvent.UpRoomCyLevel, this.onRoomUpCyLevel, this)
        oops.message.off(GameEvent.UpdateRoom, this.onUpdateRoomInfo, this);
        oops.message.off(GameEvent.ActiveRoom, this.onActiveRoom, this);


        this.node.off(PhoenixEventDefine.PHOENIX_LOGIN_SUCCESS, this.onLoginSuccess, this);
        this.node.off(PhoenixEventDefine.PHOENIX_LOGIN_FAIL, this.onLoginFail, this);
        this.node.off(PhoenixEventDefine.PHOENIX_GAME_LOGIN_SUCCESS, this.onLoginGameSuccess, this);
        this.node.off(PhoenixEventDefine.PHOENIX_GAME_LOGIN_FAIL, this.onLoginGameFail, this);
        this.unscheduleAllCallbacks();
    }

    initMessage(){
        // 客户端到服务器消息
        messageManager.registerMessage(OpcodeApp.Opcode_CS_GET_RESOURCES, ()=>{
            return new C2SMessageGetResources();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_GET_ROOM_INFO, ()=>{
            return new C2SMessageGetRoomInfo();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_GET_ROOM_PMT, ()=>{
            return new C2SMessageGetRoomPmt();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_ROOM_PMT_UP_LEVEL, ()=>{
            return new C2SMessageRoomUpLevel();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_ROOM_UP_CY_LEVEL, ()=>{
            return new C2SMessageRoomUpCyLevel();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_HARVEST_RESOURCES, ()=>{
            return new C2SMessageHarvestResources();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_CS_ACTIVE_ROOM, ()=>{
            return new C2SMessageActiveRoom();
        });

        // 服务器到客户端消息
        messageManager.registerMessage(OpcodeApp.Opcode_SC_GET_RESOURCES, ()=>{
            return new S2CMessageGetResources();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_GET_ROOM_INFO, ()=>{
            return new S2CMessageGetRoomInfo();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_GET_ROOM_PMT, ()=>{
            return new S2CMessageGetRoomPmt();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_ROOM_PMT_UP_LEVEL, ()=>{
            return new S2CMessageRoomPmtUpLevel();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_ROOM_UP_CY_LEVEL, ()=>{
            return new S2CMessageRoomUpCyLevel();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_UPDATE_RESOURCES, ()=>{
            return new S2CMessageUpdateResources();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_UPDATE_ROOM_INFO, ()=>{
            return new S2CMessageUpdateRoomInfo();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_UPDATE_ROOM_PMT, ()=>{
            return new S2CMessageUpdateRoomPmt();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_HARVEST_RESOURCES, ()=>{
            return new S2CMessageHarvestResources();
        });
        messageManager.registerMessage(OpcodeApp.Opcode_SC_ACTIVE_ROOM, ()=>{
            return new S2CMessageActiveRoom();
        });
    }
    
    onScheduleHarvest(){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_HARVEST_RESOURCES);
        this.phoenix?.sendGamePacket(packet);  
    }

    onLoginSuccess(recode:any){
        oops.message.dispatchEvent(GameEvent.LoginAccountServerSuccess, recode);
    }

    onLoginFail(recode:any){
        oops.message.dispatchEvent(GameEvent.LoginAccountServerFail, recode);
    }

    private onHandler(event: string, ...args: any) {
        switch (event) {
            case GameEvent.GameStartLogin:
                this.onStartLoginHandle(args[0], args[1]);
                break;
            case GameEvent.StartLoginGameServer:
                this.onStartLoginGameServer();
                break;
            case PhoenixEventDefine.PHOENIX_GAME_LOGIN_SUCCESS:
                this.onLoginGameSuccess(args[0]);
                break;    
            default:
                break;    
        }
    }
    
    onStartLoginHandle(account:string, passwd:string){
        this.platformLogin(account, passwd)
    }

    onStartLoginGameServer() {
        this.loginGameServer();
    }

    onLoginGameSuccess(data:any){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_GET_RESOURCES);
        this.phoenix?.sendGamePacket(packet, false);
        packet = messageManager.createPacket(OpcodeApp.Opcode_CS_GET_ROOM_INFO);
        this.phoenix?.sendGamePacket(packet, false);
        packet = messageManager.createPacket(OpcodeApp.Opcode_CS_HARVEST_RESOURCES);
        this.phoenix?.sendGamePacket(packet, false);
        this.phoenix?.sendBundle();
        this.schedule(this.onScheduleHarvest, 60);
    }

    onLoginGameFail(data:any){
        oops.message.dispatchEvent(GameEvent.AddGameMsgInfo, "登录失败: " + data.base?.msg);
    }

    onKick(data:any){
        oops.message.dispatchEvent(GameEvent.AddGameMsgInfo,  data.base?.msg);
        this.unscheduleAllCallbacks();
    }

    onUpdateGameRes(event:string, resType:string, resValue:number){
        switch (resType) {
            case EnumGameRes.GameResType_Carrot:
                (this.phoenix!.getAvatarModel()! as AvatarModel).setCarrot(resValue);
                break;
            case EnumGameRes.GameResType_MinCarrot:
                (this.phoenix!.getAvatarModel()! as AvatarModel).setMinuteCarrot(resValue);
                break;     
            default:
                break;   
        }
    }

    onGetRoomInfo(event:string, roomInfo:proto.Iroom_info[]){
        let sortRoom = roomInfo.sort((a, b) => {
            return a.roomId! - b.roomId!;
        });
        // 遍历roomInfo
        for (let i = 0; i < sortRoom.length; i++) {
            let room = sortRoom[i];
            (this.phoenix!.getAvatarModel()! as AvatarModel).setRoomInfo(room);
            oops.message.dispatchEvent(GameEvent.AddShop, room);
        }
    }

    onUpdateRoomInfo(event:string, roomInfo:proto.Iroom_info){
        (this.phoenix!.getAvatarModel()! as AvatarModel).setRoomInfo(roomInfo);
        oops.message.dispatchEvent(GameEvent.UpdateRoomShow, roomInfo);
    }

    GetRoomPmtInfo(roomId:number){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_GET_ROOM_PMT);
        packet.getMessageEntity<proto.Ic2s_GetRoomPmtInfo>().roomId = roomId;
        this.phoenix?.sendGamePacket(packet);
    }

    onRequestRoomPmtInfo(event:string, roomId:number, actived:boolean){
        if (actived && roomId){
            this.GetRoomPmtInfo(roomId);
            oops.message.dispatchEvent(GameEvent.ClearRoomPmtInfo);
        }
        if(this.switchRoom != roomId){
            this.switchRoom = roomId;
        }
    }

    onGetRoomPmtInfo(event:string, roomPmtInfo:proto.Iroom_pmt_info[]){
        let sortPmt = roomPmtInfo.sort((a, b) => {
            return a.pmtId! - b.pmtId!;
        });
        for (let i = 0; i < sortPmt.length; i++) {
            this.switchRoom = sortPmt[i].roomId!;
            let roomPmt = sortPmt[i];
            oops.message.dispatchEvent(GameEvent.AddShopPmt, roomPmt);
        }
    }

    onUpdateRoomPmtInfo(event:string, roomPmtInfo:proto.Iroom_pmt_info[]){
        
    }

    onRoomPmtUpLevel(event:string, roomId:number, pmtId:number){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_ROOM_PMT_UP_LEVEL);
        packet.getMessageEntity<proto.Ic2s_UpRoomPmtLevel>().roomId = roomId;
        packet.getMessageEntity<proto.Ic2s_UpRoomPmtLevel>().pmtId = pmtId;
        this.phoenix?.sendGamePacket(packet);
    }

    onRoomUpCyLevel(event:string, roomId:number){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_ROOM_UP_CY_LEVEL);
        packet.getMessageEntity<proto.Ic2s_UpRoomCyLevel>().roomId = roomId;
        this.phoenix?.sendGamePacket(packet);
    }

    onActiveRoom(event:string, roomId:number){
        let packet = messageManager.createPacket(OpcodeApp.Opcode_CS_ACTIVE_ROOM);
        packet.getMessageEntity<proto.Ic2s_ActiveRoom>().roomId = roomId;
        this.phoenix?.sendGamePacket(packet);
    }
}


